package xxl.robotlegs.bundles.mvcs 
{
	import flash.display.InteractiveObject;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import xxl.robotlegs.extensions.contextsignal.api.IContextSignal;
	import xxl.robotlegs.extensions.localsignalmap.api.ISignalMap;
	import xxl.robotlegs.extensions.mediatormap.api.IMediator;
	import xxl.signals.api.ISlot;
	import xxl.signals.impl.natives.sets.NativeSignalSet;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class Mediator implements IMediator 
	{
		protected var _viewComponent:Object;
		protected var _viewSignalSets:Dictionary = new Dictionary(true);
		
		[Inject]
		public var signalMap:ISignalMap;
		
		[Inject]
		public var contextSignal:IContextSignal;
		
		public function Mediator() 
		{
			
		}
		
		public function set viewComponent(value:Object):void 
		{
			_viewComponent = value;
		}
		
		public function postDestroy():void
		{
			signalMap.unmapListeners();
		}
		
		//----------protected
		final protected function addViewListener(target:IEventDispatcher, eventType:String, listener:Function, once:Boolean = false):ISlot
		{
			_viewSignalSets[target] ||= new NativeSignalSet(target, true);
			const nss:NativeSignalSet = _viewSignalSets[target];
			return signalMap.mapListener(nss.getNativeSignal(eventType), listener, once);
		}
		
		final protected function removeViewListener(target:IEventDispatcher, eventType:String, listener:Function):ISlot
		{
			const nss:NativeSignalSet = _viewSignalSets[target];
			return nss?signalMap.unmapListener(nss.getNativeSignal(eventType), listener):null;
		}
		
		final protected function addContextListener(key:Object, listener:Function, once:Boolean = false):ISlot
		{
			return signalMap.mapListenerWithKey(contextSignal, key, listener, once);
		}
		
		final protected function removeContextListener(key:Object, listener:Function):ISlot
		{
			return signalMap.unmapListenerWithKey(contextSignal, key, listener);
		}
		
		final protected function addViewClickListener(target:InteractiveObject, listener:Function, once:Boolean = false):ISlot
		{
			return addViewListener(target, MouseEvent.CLICK, listener, once);
		}
		
		final protected function removeViewClickListener(target:InteractiveObject, listener:Function):ISlot
		{
			return removeViewListener(target, MouseEvent.CLICK, listener);
		}
		
		/* INTERFACE xxl.robotlegs.extensions.mediatormap.api.IMediator */
		
		public function initialize():void 
		{
			
		}
		
		public function destroy():void 
		{
			
		}
		
	}

}